package org.jk.jkdao.test;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.InvalidXPathException;
import org.dom4j.XPath;
import org.dom4j.dom.DOMDocument;
import org.dom4j.io.SAXReader;
import org.w3c.dom.NodeList;

public class ConfigurationXML {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Document document;
		
		SAXReader xmlreader = new SAXReader();
		
		try {
			document = xmlreader.read("jkdao.xml");
			XPath xpathselector = DocumentHelper.createXPath("/jkdao/connection/param");
			List list = xpathselector.selectNodes(document);
			Iterator ite = list.iterator(); 
			System.out.println(list.size());
			while(ite.hasNext()){
				Element element = (Element) ite.next();
				
				System.out.println("Element " + element.getName() + element.getStringValue());
				
				Attribute attribute = element.attribute("tst");
				if(attribute==null) System.out.println("null");
				else System.out.println(attribute.getText());
				System.out.println(element.getPath() + " ; " + element.elementText("user"));
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		catch(InvalidXPathException ixpe){
			ixpe.printStackTrace();
		}
		
		
		
	}
	
	private static void DOMTest(){
		
		DOMDocument domdocument = new DOMDocument("jkdao.xml");
		
		NodeList nodelist = domdocument.getChildNodes();
		
		System.out.println(domdocument.getLocalName()+":"+ nodelist.getLength());
		
		
	}
	

}
